package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.article.mess.ArticleVisitStreamMess;
import com.heima.model.mess.UpdateArticleMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 监听kafkastream流式计算后的行文数据结果,用于更新db中文章中的相关数据以及redis中缓存的热点文章数据
 */
@Slf4j
@Component
public class ArticleIncrHandlerListener {
    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void onMessage(String msg) {
        if (StringUtils.isNotBlank(msg)) {
            log.info("接收到用户行为发送的kafka消息,开始计算相关文章分值....");

            ArticleVisitStreamMess mess = JSON.parseObject(msg, ArticleVisitStreamMess.class);
            apArticleService.updateScore(mess);

            log.info("接收到用户行为发送的kafka消息,计算相关文章分值over....");
        }
    }
}
